/* 
 *
 * Announcement js functions
 *
 * elements (Event Handler)
 * 
 */
    announcement = {
        postAnnouncement : function(elements){
            $("body").on("click",elements, function(){
                /* Company ID */
                companyID = $(this).attr("data-company-id");
                /* Type of Post */
                postType = $(this).attr("data-action-post-type");
                type = postTypeF(postType); // Change the value of attribute into numeric
            /* Determine if the attribute is not null */
            if(postType!=""){
                /* Set the date */
                    /* function created for the date dateTimeDate() */
                    var dateTime = dateTimeDate();
                /* Replacing comment if it is contains some bad words */
                    var getPost = post(".announcement","",postType);
                /* Show loader icon */    
                    $("#flash").show();
                /* Disabled the button if the processed is not done */    
                    $(elements).attr("disabled",true);
                /* Validation if post field is null */
                    if(getPost!=""){
                        /* Set pause time before saving the post to the database */
                        setTimeout(function(){
                            $.ajax({
                                type: "POST",
                                url : "/announcement/"+getPost+"/"+type+"/"+dateTime+"/"+companyID,
                                success : function(data){
                                    showNotification({
                                        message: "Please type your prefered announcement above.",
                                            type: "success",
                                       autoClose: true,
                                       duration: 3
                                    });
                                    $(".announcement").val(null); // Remove value of the post field
                                    //$(data).prependTo(".postHere").fadeIn().show(); // Append/show newly added post to the web)
                                    //$(data).insertAfter(".postHere");
                                    $(data).fadeIn("slow").prependTo(".postHere");
                                    $(elements).attr("disabled",false);
                                    $("label.timeago").timeago();  // Time To go
                                    $("#flash").hide(); 
                                }
                            })
                        },2000);
                    }else{
                        showNotification({
                            message: "Please type your prefered announcement above.",
                                type: "error",
                           autoClose: true,
                           duration: 3
                        });
                        $("#flash").hide();
                        $(elements).attr("disabled",false);
                    }
            }
            });
        },
        // Load Announcement
        getAnnouncementPost : function(elements){
            $.ajax({
                type : "GET",
                url : "/announcement",
                success : function(data){
                    $(".postHere").html(data);
                    $("label.timeago").timeago();  // Time To go
                }
            });
        },
        // Load Comment
        getCommentPost : function(elements){
            var action = "loadComment";
            $.ajax({
                type : "GET",
                url : "/announcement/"+action,
                success : function(e){
                    $(".appendNewComment").html(e);   
                    $("label.timeago").timeago();  // Time To go
                }
            });
        },
        // Show Reply field
        showReply : function(){
            $("body").on("click",".showReply", function(){
                postType = $(this).attr("data-action-post-type");
                
                // Determine of showing the field for reply
                //postTypeF(postType,"show",".showReply"); //function of type
                //alert(postType+"\n"+id);
                if(postType=="rTP"){
                    id = $(this).attr("data-announcement-id");
                }else{
                    id = $(this).attr("data-comment-id");
                }
                $("#showReply_"+id+"_"+postType).fadeIn().show();
            });
        },
        // Add Comment
        addComment : function(elements){
            /* action button Comment */
            $("body").on("click",elements, function(){
                // Determine of showing the field for reply
                if(postType=="rTP"){
                    id = $(this).attr("data-announcement-id");
                }else{
                    id = $(this).attr("data-comment-id");
                }
                /* Type of Post */
                    postType = $(this).attr("data-action-post-type");
                    type = postTypeF(postType); // Change the value of attribute into numeric
                /* Set the date */
                        /* function created for the date dateTimeDate() */
                        var dateTime = dateTimeDate(); 
                
                /* Replacing comment if it is contains some bad words */
                        var getPost = post("#newComment",id,postType);
                /* Determine if the attribute is not null */
                if(postType!=""){
                        /* Validation if post field is null */
                            if(getPost!=""){
                                $.ajax({
                                    type: "POST",
                                    url : "/announcement/"+getPost+"/"+type+"/"+dateTime+"/"+id,
                                    success : function(data){
                                        showNotification({
                                            message: "Your Comment was successfully saved.",
                                                type: "success",
                                           autoClose: true,
                                           duration: 3
                                        });
                                        $("#newComment_"+id+"_"+postType).val(null);
                                        $("#appendNewComment_"+id+"_"+postType).append(data);   
                                        $("#loadingComment_"+id).hide();
                                        $("label.timeago").timeago();  // Time To go
                                    }
                                });
                            }else{
                                showNotification({
                                    message: "Please type your prefered comment above.",
                                        type: "error",
                                    autoClose: true,
                                    duration: 3
                                });
                                $("#loadingComment_"+id).hide();
                            }
                    
                }
            });
        },
        // Delete announcement
        deleteRecord : function(){
            $("body").on("click",".deletePost, .deleteNewComment",function(){
                con =  "Are you sure do you want to delete this post?";
                action =  $(this).attr("data-action");
                // Get ID by action
                if(action=="deleteComment"){
                    id = $(this).attr("data-comment-id");
                }else{
                    id = $(this).attr("data-announcement-id");
                }
                if(confirm(con)){
                    $.ajax({
                       type : "POST",
                       url  : "/announcement/"+id+"/"+action,
                       success : function(e){
                            if(e=="Announcement was successfully deleted."){
                                showNotification({
                                    message: e,
                                    type: "success",
                                    autoClose: true,
                                    duration: 3
                                });
                            }else{
                                if(e=="Selected comment was successfully deleted."){
                                    showNotification({
                                        message: e,
                                        type: "success",
                                        autoClose: true,
                                        duration: 3
                                    });
                                }
                            }
                       }
                    });
                    if(action=="deletePost"){
                        $(this).parents("#myPost_"+id).animate({ backgroundColor: "whiteSmoke" }, "fast")
                            .animate({ opacity: "hide" }, "slow");
                    }else{
                        $(this).parents("#deleteComment_"+id).animate({ backgroundColor: "whiteSmoke" }, "fast")
                            .animate({ opacity: "hide" }, "slow");
                        
                    }
                    
                }
            });
        }
    }
$(document).ready(function(){
});
/* Date Functions
 *
 * Set the date of the post and for the comment
 * dateTimeDate();
 * 
 */
    function dateTimeDate(){
        var d = new Date(); 
        return d.getFullYear() + '-' + ('0' + (d.getMonth()+1)).slice(-2) + '-' + ('0' + d.getDate()).slice(-2) + ' ' + d.getHours() + ':' + ('0' + d.getMinutes()).slice(-2) + ':' + ('0' + d.getSeconds()).slice(-2);
    }
/* Function type for showing reply field for the post
 *
 * (Type of Post)
 * A (rTP=1) reply to Post= to post
 * B (rTCP=2) reply to the comment of post
 * C (aP=3) announcePost for the new announcement
 *
 * postTypeF()
 * @type - determine type of post on the function
 * @event - eventhandler / elements of an action
 *
 * 
 */
    /* Showing Field */
    function showPostTypeF(type,event){
        // Showing field to make comment
        if(type=="rTP"){
            id = $(event).attr("data-announcement-id"); // Announcement ID
            return $("#showReply_"+id+"_"+type).slideDown();
        }else{
            if(type=="rTCP"){
                id = $(event).attr("data-comment-id"); // Comment ID
                return $("#showReply_"+id+"_"+type).slideDown();
            }else{
                return "";
            }
        }
    }
    /* Change Value into numeric */
    function postTypeF(type){
        if(type=="rTP"){
            ret = "1";
        }else{
            if(type=="rTCP"){
                ret = "2";
            }else{
                if(type=="aP"){
                    ret = "3";
                }else{
                    ret = "";
                }
            }
        }
        return ret;
    }
/*
 * Function for bad words in the
 * field.
 *
 */
    function post(event,id,type){
        t = postTypeF(type);
        if(type=="aP"){
            a = $(event);
        }else{
            a = $(event+"_"+id+"_"+type);
        }
            var post = a.val().replace(new RegExp("gago","g"),"****").replace(new RegExp("peste","g"),"****").
            replace(new RegExp("putangina mo","g"),"****").replace(new RegExp("putanginamo","g"),"****").replace(new RegExp("fuck","g"),"****").
            replace(new RegExp("ass hole","g"),"****");
            
        return post;
    }


